package org.snowin.utils;


import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import java.util.ArrayList;
/**
 * @Description 邮件类
 * @author young
 * @version V0.0.1
 * @date 2017-09-08
 */
public class SendMail {
    private LogUtil logmail =new LogUtil(SendMail.class);
    public static void main(String[] args) {
        SendMail sendMail=new SendMail();
        sendMail.sendmessage("yongwy@xiaoshouyi.com", "PAq!@#00", "smtp.exmail.qq.com", "25", "yongwy@xiaoshouyi.com","yanglin@xiaoshouyi.com,yongwy@xiaoshouyi.com", "Spring Boot框架自动部署测试", "发票收款UI-automate 邮箱测试;  \n勿回");
        }
    /**
     *
     * @param userName  发送邮箱账号  xxx@xxx.com形式(邮箱账号)
     * @param passWord  发送邮件密码(邮箱密码)
     *
     * @param smtpHost  stmp服务器地址
     * @param smtpPort  smtp服务器端口
     *
     * @param from      发件人地址
     * @param tos       收件人地址
     *
     * @param title     标题
     * @param content   内容
     *
     * @param attachFileNames 附件内容的文件名
     */
    public void sendmessage(String userName,String passWord,String smtpHost,String smtpPort,String from,String tos ,String title,String content)
    {
        Properties smtpProper=setSmtp(smtpHost, smtpPort, userName);
        Auth authenticator=new Auth(userName, passWord);
        Transport transport = null;
        try {

            //创建session实例
//          Session session = Session.getDefaultInstance();
            Session session=Session.getInstance(smtpProper, authenticator);
            MimeMessage message=new MimeMessage(session);
            session.setDebug(true);

            //设置from发件人邮箱地址
            message.setFrom(new InternetAddress(from));
            //收件人to LIST
            ArrayList<Address> toList=new ArrayList<Address>();
            //收件人字符串通过,号分隔收件人
            String[] toArray=tos.split(",");
            for (int i = 0; i < toArray.length; i++)
            {
                String str = toArray[i];
                toList.add(new InternetAddress(str));
            }
            //将收件人列表转换为收件人数组
            Address[] addresses=new Address[toList.size()];
            addresses=toList.toArray(addresses);
            //设置to收件人地址
            message.setRecipients(MimeMessage.RecipientType.TO,addresses);

            message.setRecipients(MimeMessage.RecipientType.CC,addresses);
            //设置邮件标题
            message.setSubject(title);
            //设置邮件内容
            message.setContent(content, "text/html;charset=UTF-8");

//            message.saveChanges();// implicit with send()
            transport=session.getTransport();
            transport.connect(smtpHost,userName, passWord);
            transport.sendMessage(message,addresses);
//            transport.sendMessage(message,message.getAllRecipients());
            logmail.info("发送邮件成功！");
            transport.close();
        } catch (Exception e) {
            // TODO: handle exception
            logmail.error("发送邮件失败！");
            e.printStackTrace();
        }


    }

    private Properties setSmtp(String smtpHost,String smtpPort,String userName)
    {
        //创建邮件配置文件
        Properties maiProperties=new Properties();
        //配置smtp发件服务器
        maiProperties.put("mail.transport.protocol", "smtp");
        //配置smtp服务器
        maiProperties.put("mail.smtp.host", smtpHost);
        //配置smtp服务器端口
        maiProperties.put("mail.smtp.port", smtpPort);
        //配置smtp用户名
        maiProperties.put("mail.user", userName);



        //配置smtp身份验证
        maiProperties.put("mail.smtp.auth", "true");
        //配置 startttls
//        maiProperties.put("mail.smtp.starttls.enable", "true");
        return maiProperties;


    }

}
//smtp 身份验证类
class Auth extends Authenticator
{
    Properties pwdProperties;
    //构造函数

    public Auth(String userName,String passWord)
    {
        pwdProperties=new Properties();
        try {
            pwdProperties.put(userName,passWord);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    //必须实现 PasswordAuthentication 方法
    public PasswordAuthentication passwordAuthentication()
    {
        String userName=getDefaultUserName();
        //当前用户在密码表里
        if (pwdProperties.containsKey(userName)) {
            //取出密码，封装好后返回
            return new PasswordAuthentication(userName, pwdProperties.getProperty(userName).toString());

        }
        else {
            //如果当前用户不在密码表里就返回Null
            System.out.println("当前用户不存在");
            return null;


        }

    }


}


